package felix.things.router.modbus.net;

import com.ghgande.j2mod.modbus.io.AbstractModbusTransport;
import felix.things.router.modbus.transport.RtuTransportConfiguration;
import felix.things.router.modbus.transport.TcpTransportConfiguration;
import felix.things.router.modbus.transport.TransportConfiguration;
import felix.things.router.modbus.transport.UdpTransportConfiguration;

/**
 * @author Felix
 * @date 2019/10/30
 **/
public class ModbusMasterFactory {
    /**
     * 获取AbstractModbusTransport
     *
     * @return
     */
    public static AbstractModbusTransport createModbusTransport(TransportConfiguration configuration) {
        if (configuration instanceof TcpTransportConfiguration) {
            return TcpModbusMasterFactory.createModbusTransport((TcpTransportConfiguration) configuration);
        } else if (configuration instanceof UdpTransportConfiguration) {
            return UdpModbusMasterFactory.createModbusTransport((UdpTransportConfiguration) configuration);
        } else if (configuration instanceof RtuTransportConfiguration) {
            return RtuModbusMasterFactory.createModbusTransport((RtuTransportConfiguration) configuration);
        } else {
            throw new IllegalArgumentException("unknown config " + configuration.getClass());
        }
    }
}
